class EventBus {
    constructor() {
        this.events = {};
    }

    emit(type, ...args) {
        const e = this.events[type];
        if (!e) return;
        if (Array.isArray(e)) {
            for (let i = 0; i < e.length; i += 1) {
                e[i].apply(this, args);
            }
        } else {
            e[0].apply(this, args);
        }
    }

    addListener(type, fn) {
        const e = this.events[type];
        if (!e) {
            this.events[type] = [fn];
        } else {
            e.push(fn);
        }
    }

    removeListener(type, fn) {
        if (!fn) {
            delete this.events[type];
        } else if (this.events[type]) {
            this.events[type] = this.events[type].filter(item => item !== fn);
        }
    }
}
export default new EventBus();
